package com.mobileagri.backoffice.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
 * Romain LOUVET TEST Contact DAO class.
 * 
 * 
 * @author LOUVET Romain
 */
@Repository
public class AbstractDAO {

	private HibernateTemplate hibernateTemplate;

	private SessionFactory sessionFactory;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	/**
	 * Get List of contacts from database
	 * 
	 * @return list of all contacts
	 */
	@SuppressWarnings("unchecked")
	public List<Object> getObjects(Object o, int start, int limit) {

		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());

		return hibernateTemplate.findByCriteria(criteria, start, limit);
	}

	/**
	 * Get List of contacts from database
	 * 
	 * @return list of all contacts
	 */
	@SuppressWarnings("unchecked")
	public List<Object> getObjects(Object o, int start, int limit, String sort) {

		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());

		if (sort.toLowerCase().equals("desc")) {
			criteria.addOrder(Order.desc("id"));
		} else if (sort.toLowerCase().equals("asc")) {
			criteria.addOrder(Order.asc("id"));
		}

		return hibernateTemplate.findByCriteria(criteria, start, limit);
	}

	// /**
	// * Get List of contacts from database
	// *
	// * @return list of all contacts
	// */
	// @SuppressWarnings("unchecked")
	// public List<Contact> getContacts() {
	//
	// // DetachedCriteria criteria = DetachedCriteria.forClass(Contact.class);
	//
	// String sqlQuery = " from Contact";
	//
	// return (List<Contact>) hibernateTemplate.find(sqlQuery);
	//
	// // return hibernateTemplate.find("SELECT COUNT(*) FROM Contact",
	// // Contact.class);
	//
	// // return hibernateTemplate.findByCriteria(criteria, start, limit);
	// }

	/**
	 * Delete a contact with the id passed as parameter
	 * 
	 * @param id
	 */
	public void delete(Object o, int id) {
		Object record = hibernateTemplate.load(o.getClass(), id);
		hibernateTemplate.delete(record);
	}

	/**
	 * Create a new Contact on the database or Update contact
	 * 
	 * @param contact
	 * @return contact added or updated in DB
	 */
	public Object save(Object o) {
		hibernateTemplate.saveOrUpdate(o);
		return o;
	}

	/**
	 * Get total of Contacts from database
	 * 
	 * @return
	 */
	public int getTotalObject() {
		return DataAccessUtils.intResult(hibernateTemplate
				.find("SELECT COUNT(*) FROM Contact"));
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
